<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    select{
      width: 200px;
      height: 40px;
      line-height: 40px;
      font-size: 16px;
      border: 1px solid #555;
      border-radius: 4px;
      padding-left: 5px;
    }
    button{
      margin-left: 30px;
      height: 40px;
      line-height: 40px;
      border: 0;
      background-color: rgb(28, 160, 248);
      color: #fff;
      font-size: 16px;
      width: 100px;
      border-radius: 4px;
    }
  </style>
</head>
<body>
  <select id="select">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
  </select>
  <button id="button">点我随机</button>
</body>
<script>
  /*
  设置下拉框中的默认随机选中项，自己书写结构和js。
  */
 window.onload = function(){
  var select = document.getElementById("select");
  var options = select.children;
  var button = document.getElementById("button");
  //当前的指针指向
  var currentNum = -1;

  randomSelect();

  button.onclick = randomSelect;

  function randomSelect(){
    if(currentNum != -1){
      //首先移除上一个被选中的selected
      options[currentNum].removeAttribute("selected");
    }

    //随机下标
    currentNum = Math.floor(Math.random()*options.length);
    //为其赋值
    options[currentNum].setAttribute("selected","selected");
  }
 }
</script>
</html>